package com.yangguang.huahui.ui.adapters;

import android.app.AlertDialog;
import android.graphics.Color;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.yangguang.huahui.R;
import com.yangguang.huahui.baseadapter.BaseMultiItemQuickAdapter;
import com.yangguang.huahui.baseadapter.BaseViewHolder;
import com.yangguang.huahui.entity.Guige;
import com.yangguang.huahui.entity.SelectEntity;

import java.util.List;

/**
 * Created by Administrator on 2017/3/24.
 */

public class GuigeAdapter extends BaseMultiItemQuickAdapter<SelectEntity> {
    private OnItemClick onItemClick;
    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public GuigeAdapter(List<SelectEntity> data) {
        super(data);
        addItemType(1, R.layout.grid_normal_list_head_item);
        addItemType(2, R.layout.grid_normal_list_item);
    }

    @Override
    protected void convert(final BaseViewHolder helper, final SelectEntity item) {
        switch (getItemViewType(helper.getPosition())) {
            case 1:
                helper.setText(R.id.tv_content, item.getName());
                break;
            case 2:
                helper.setText(R.id.tv_content, item.getName());
                helper.getConvertView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onItemClick.onItemClick(helper.getPosition(), item.getId(), item.getName());
                    }
                });
                break;
        }
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if(manager instanceof GridLayoutManager) {
            final GridLayoutManager gridManager = ((GridLayoutManager) manager);
            gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    return getItemViewType(position) == 1 ? gridManager.getSpanCount() : 1;
                }
            });
        }
    }

    public interface OnItemClick{
        void onItemClick(int position, String id, String name);
    }
    public void setOnItemClick(OnItemClick onItemClick){
        this.onItemClick = onItemClick;
    }
}
